<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Payment extends Controller
{
	private $_template_f;
	private $_lang;
	private $_numItem = 20;
	function __construct()
	{
		parent::Controller();
		$this->_lang = $this->mycommon->getLangCode();
		$this->load->language('common', $this->_lang);
		$this->_template_f = $this->config->item('template_f');
		$common_lang = $this->lang->line('common_lang');
		if(!$this->session->userdata('uname'))
		{
			redirect(site_url('/login'));
			die();
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				show_error($common_lang['access_denied']);
				die();
			}
		}
		
		$this->load->model('payment/payment_model');
		$this->load->language('common', $this->_lang);
		$this->load->language('payment/payment', $this->_lang);
	}
	
	function index()
	{
		$this->listCod();
	}
		
	function listCod()
	{
		$data = array();
		
		$uid = (int)$this->session->userdata('uid');
		$permit = (int)$this->session->userdata('permitid');
		
		$status = $this->input->get('st', true);
		if($status == '' || !is_numeric($status)) $status = -1;
		$status = (int)$status;
		
		$type = (int)$this->input->get('type', true);
		
		$provice = $this->input->get('pvc', true);
		if($provice == '' || !is_numeric($provice)) $provice = -1;
		$provice = (int)$provice;
		
		$distric = $this->input->get('dst', true);
		if($distric == '' || !is_numeric($distric)) $distric = -1;
		$distric = (int)$distric;
		
		$page = (int)$this->input->get('page', true);
		if($page < 1) $page = 1;
		
		$data['items'] = $this->payment_model->cod_bill_getall($uid,$permit,$provice,$status,$distric,$type,$page,$this->_numItem);
		$data['page'] = $page;
		$data['pages'] = (int)$data['items']['pages'];
		$data['items'] = $data['items']['items'];
		$data['lang'] = $this->lang->line('payment');
		$data['distric'] = $distric;
		$data['provice'] = $provice;
		$data['lstDistrict'] = $this->payment_model->districts_get_all_by_provinceid($provice);
		$data['status'] = $status;
		$data['type'] = $type;
		$data['lstProvice'] = $this->payment_model->province_get_all();

		$this->_loadHeader();
		$this->_loadMenu();
		
		$this->load->view($this->_template_f.'payment/cod_getall', $data);
		
		$this->_loadFooter();
	}
	
	function districts_get_all()
	{
		$id = (int)$this->input->get('id', true);
		$lang = $this->lang->line('payment');
		$html = '<option value="-1">'.$lang['select_all'].'</option>';
		if($id <= 0) echo $html;
		else
		{
			$items = $this->payment_model->districts_get_all_by_provinceid($id);
			if(count((array)$items) > 0) foreach ($items as $item) {
				$html .= '<option value="'.$item->id.'">'.$item->title.'</option>';
			}
			echo $html;
		}
	}
	
	function updateAction()
	{
		$id = (int)$this->input->post('id', true);
		$action = (int)$this->input->post('action', true);
		if(!in_array($action, array(0,1))) return;
		$uid = (int)$this->session->userdata('uid');
		$this->payment_model->updateAction($uid, $id, $action);
	}
	
	function addMoney()
	{
		$id = (int)$this->input->get('id', true);
		$item = $this->payment_model->cod_bill_get($id);
		if(count((array)$item) <= 0) return;

		$data = array();
		$data['lang'] = $this->lang->line('payment');
		
		//Nap tien
		if(isset($_POST['money']) && $item->status != 2)
		{
			$money = (int)str_replace(',','',$this->input->post('money',true));
			if($money > 0 && false)	//disable
			{
				$key = $this->payment_model->getPaymentKey();
				$cusid = (int)$item->userid;
				$uid = (int)$this->session->userdata('uid');
				$typepay = ($item->type == 0 ? 'COD Bill' : 'ATM - Bank transfer');
				if($item->type == 0)
				{
					$note = $data['lang']['fullname'].': '.$item->name.', '.$data['lang']['address'].
							': '.$item->address.'('.$item->districtname.' / '.$item->provincename.'), '.
							$data['lang']['email'].': '.$item->email.', '.$data['lang']['phone'].': '.$item->phone;
				}
				else
				{
					$note = $data['lang']['fullname'].': '.$item->name.', '.$data['lang']['email'].': '.$item->email.', '.$data['lang']['phone'].': '.$item->phone;
				}
				$this->payment_model->addMoney($cusid,$key,$money,$note,$typepay,$uid,$item->id);
				
				//Sendmail
				$user = $this->payment_model->get_user_info($cusid);
				ob_start();
				printf($data['lang']['email_content_pay'],$user->username,number_format($money));				
				$content = ob_get_contents();
				ob_end_clean();
				$this->mycommon->sendmail(array(
					'to'	=>	$user->email_address,
					'subject'	=>	$data['lang']['email_subject_pay'],
					'body'	=>	$content
				));
				
				echo json_encode(array('status'=>1,'mess'=>$data['lang']['add_money_success']));
				return;
			}
			echo json_encode(array('status'=>0,'mess'=>$data['lang']['add_money_not_success']));
			return;
		}
		
		$data['item'] = $item;
		$this->load->view($this->_template_f.'payment/pay_add_confirm', $data);
	}
	
	function _loadHeader($title = '')
	{
		$header = array();
		$header['title'] = $title == '' ? 'Self serving' : $title;
		$header['template_f'] = $this->_template_f;
		
		$header['username'] = '';
		$header['userid'] = '';
		$header['permitid'] = '';
		$header['lang'] = $this->_lang;
		$header['module_name'] = 'home';
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
		
		// load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
		
		$this->load->view($this->_template_f . 'menu_view', $data);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
}